import axios from 'axios'
import store from '@/store'
import router from '@/router'
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import {
  Toast
} from 'vant'

const request = axios.create({
  baseURL: 'http://192.168.14.119:28051'
})

// 请求拦截器
request.interceptors.request.use((config) => {
  Nprogress.start()
  if (location.hash == '#/login' && config.methos == 'get') {
    return config
  }
  if (!store.getters.token) {
    // token不存在
    router.push('/login')
  } else {
    return config
  }
  return config
}, (error) => {
  return Promise.reject(error)
})

request.interceptors.response.use(response => {
  Nprogress.done()
  // console.log(response);
  if ((response.status !== 200 || response.status !== 201) && response.request.readyState !== 4) {
    Toast.fail("数据请求失败，请稍后重试")
    // console.log(1);
  }
  return response.data
})

export default request
